#include "dd_plane.h"


#include "util/mmgr.h"		// Needs to come in early


using namespace dd;

Plane::Plane()
{

}

Plane::~Plane()
{

}

void Plane::Render(DBG_BUFFER* _pBuffer)
{
	math::Vec3		X = m_Plane.GetRight() * m_Size.x * 0.5f ;
	math::Vec3		Y = m_Plane.GetUp();
	math::Vec3		Z = m_Plane.GetForward() * m_Size.z * 0.5f;
	math::Vec3		Pos = m_Pos - X - Z;
				X *= 2.0f;
				Z *= 2.0f;

	Y.Normalize();	// could be non-normalized
	if (m_RenderSolid)
	{
		// Draw the solid as well
		FlushBuffer(_pBuffer);

		COLOR Color = ColorRGBA( 255, 58, 58, 128);
		// prime side
		AddLine(_pBuffer, Pos,  Pos + X, Color);
		AddLine(_pBuffer, Pos + X + Z, Pos, Color);
		AddLine(_pBuffer, Pos + X + Z, Pos + Z, Color);

		// And reverse side as well
		AddLine(_pBuffer, Pos + X + Z,  Pos + X, Color);
		AddLine(_pBuffer, Pos, Pos + Z, Color);
		AddLine(_pBuffer, Pos + X + Z, Pos, Color);


		_pBuffer->pGraphics->SetRenderState(Graphics::RS_ZWRITE,		false);
		FlushBufferTri(_pBuffer);
		_pBuffer->pGraphics->SetRenderState(Graphics::RS_ZWRITE,		true);

	}
	// Render outline
	AddLine(_pBuffer, Pos,  Pos + X, m_Color);
	AddLine(_pBuffer, Pos + X,  Pos + X + Z, m_Color);
	AddLine(_pBuffer, Pos + X + Z, Pos + Z, m_Color);
	AddLine(_pBuffer, Pos + Z, Pos, m_Color);
}

void Plane::Setup(const math::Vec3& _Pos, const math::Vec3& _Size, const math::Plane& _Plane, COLOR _Color, bool _RenderSolid)
{
	m_Color = _Color;
	m_Size	= _Size;
	m_Plane	= _Plane;
	m_Pos	= _Pos;
	m_RenderSolid = _RenderSolid;
}
